package com.sskj.fabi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.sskj.common.base.App;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.presenter.OrderRecordActivityPresenter;
import com.sskj.fabi.util.TipFabiUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.LiveDataBus;

import java.util.ArrayList;

import butterknife.BindView;

@Route(path = RConfig.FABI_ORDER_RECORD)
public class OrderRecordActivity extends BaseActivity<OrderRecordActivityPresenter> {

    @BindView(R2.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @Autowired
    boolean isPublish = false;
    private String[] titles = new String[]{"订单记录", "发布订单"};
    private BottomSheetDialog statusBottomDialog;
    private BottomSheetDialog typeBottomDialog;


    @Override
    protected int getLayoutId() {
        return R.layout.fabi_activity_order_record;
    }

    @Override
    public OrderRecordActivityPresenter getPresenter() {
        return new OrderRecordActivityPresenter();
    }

    @Override
    protected void initView() {

        ARouter.getInstance().inject(this);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_ORDER_RECORD).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_PUBLISH_RECORD).navigation());
        slidingTabLayout.setViewPager(viewPager, titles, this, fragments);
        ArrayList<String> statusList = new ArrayList<>();
        ArrayList<String> typeList = new ArrayList<>();
        statusList.add("未付款");
        statusList.add("已付款");
        statusList.add("已完成");
        statusList.add("申诉中");
        statusList.add("已取消");
        typeList.add("买入");
        typeList.add("卖出");
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                isPublish = position == 1;
            }
        });
        slidingTabLayout.setCurrentTab(isPublish ? 1 : 0);
        setRightImg(R.mipmap.fabi_icon_filter, v -> {
            if (isPublish) {
                if (typeBottomDialog == null) {
                    typeBottomDialog = TipFabiUtil.showFilterBottom(this, "委托类型", typeList, (str) -> {
                        typeBottomDialog.dismiss();
                        LiveDataBus.get().with(RxBusCode.CHANGE_FABI_PUBLISH_RECORD_TYPE, Integer.class).postValue(Integer.valueOf(str[0]) + 1);
                    });
                }
                typeBottomDialog.show();
            } else {
                if (statusBottomDialog == null) {
                    statusBottomDialog = TipFabiUtil.showFilterBottom(this, "订单状态", statusList, (str) -> {
                        statusBottomDialog.dismiss();
                        LiveDataBus.get().with(RxBusCode.CHANGE_FABI_ORDER_RECORD_TYPE, Integer.class).postValue(Integer.valueOf(str[0]) + 1);
                    });
                }
                statusBottomDialog.show();

            }
        });

    }

}
